package com.luoqiu.yygh.service.impl;

import com.luoqiu.yygh.cmn.client.DictFeignClient;
import com.luoqiu.yygh.common.result.Result;
import com.luoqiu.yygh.model.hosp.Hospital;
import com.luoqiu.yygh.model.hosp.HospitalSet;
import com.luoqiu.yygh.repos.HospitalRepos;
import com.luoqiu.yygh.service.HospitalService;
import com.luoqiu.yygh.service.HospitalSetService;
import com.luoqiu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepos hospitalRepos;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public Result<Object> save(Hospital hospital) {
        HospitalSet dbSet = hospitalSetService.selectByCode(hospital.getHoscode());
        String dbSignKey = dbSet.getSignKey();

        Hospital dbHos = hospitalRepos.readHospitalByHoscode(dbSet.getHoscode());
        if (null != dbHos) {
            hospital.setCreateTime(dbHos.getCreateTime());
        }

        hospital.setLogoData(hospital.getLogoData().replaceAll(" ", "+"));
        if (dbSignKey != null) {
            hospitalRepos.save(hospital);
        }
        return Result.ok();
    }

    @Override
    public Object selectPage(long page, long limit, HospitalQueryVo queryVo) {
        Hospital hospital = new Hospital();
        hospital.setHosname("洛秋私塾");
        hospital.setCreateTime(new Date());
        Result<String> education = dictFeignClient.getName("Education", "30000");
        HashMap<String, Object> map = new HashMap<>();
        map.put("hos", hospital);
        map.put("name", education.getData());
        return map;
    }
}
